﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using SQLiteEx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace CommonLibrary
{
    /// <summary>
    /// 产品
    /// </summary>
    [Table("products")]
    public class ProductInfo : IProduct
    {
        private static string EMPTY_VALUE = "-";
        private static Dictionary<string, short> NGCODE_DESC_DIC = new Dictionary<string, short>() {
            { "扫码NG",(short)10 },
            { "氦检NG",(short)20 },
            { "耐压NG",(short)30 },
            { "耐压接触不良NG",(short)35 },
            { "绝缘NG",(short)40 },
            { "绝缘接触不良NG",(short)45 },
            { "出站NG",(short)50 }
        };
        /// <summary>
        /// 序号
        /// </summary>
        [PrimaryKey]
        [AutoIncrement]
        [Description("序号")]
        public long Id { get; set; }
        /// <summary>
        /// 电芯产品条码
        /// </summary>
        [Description("产品条码")]
        [GUIColumnWidth(Width = 250)]
        public string BarCode { get; set; } = string.Empty;
        /// <summary>
        /// 进站时间
        /// </summary>
        [Description("进站时间")]
        [GUIColumnWidth(Width = 250)]
        public string StartTime { get; set; } = string.Empty;
        /// <summary>
        /// 出站时间
        /// </summary>
        [Description("出站时间")]
        [GUIColumnWidth(Width = 250)]
        public string EndTime { get; set; } = string.Empty;
        /// <summary>
        /// 最终判定
        /// </summary>
        [Description("最终判定")]
        [GUIColumnWidth(Width = 350)]
        public string NGCode
        {
            get
            {
                List<string> ngs = new List<string>();
                short shortValue = 0;
                string result = EMPTY_VALUE;
                if (!string.IsNullOrWhiteSpace(this.Quality) && this.Quality.Equals("100"))
                {
                    result = "OK";
                }
                else if (!string.IsNullOrWhiteSpace(this.Quality) && !this.Quality.Equals("100") && short.TryParse(this.Quality, out shortValue))
                {
                    ngs.Add(GetNGCodeDescByValue(shortValue));
                    if (!string.IsNullOrWhiteSpace(this.HeliumResult) && !this.HeliumResult.Equals(EMPTY_VALUE) && !this.HeliumResult.Equals("OK"))
                    {
                        if (short.TryParse(this.HeliumResult, out shortValue))
                        {
                            ngs.Add(GetNGCodeDescByValue(shortValue));
                        }
                        else
                        {
                            ngs.Add(this.HeliumResult);
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(this.ST5520Result) && !this.ST5520Result.Equals(EMPTY_VALUE) && !this.ST5520Result.Equals("OK"))
                    {
                        if (short.TryParse(this.ST5520Result, out shortValue))
                        {
                            ngs.Add(GetNGCodeDescByValue(shortValue));
                        }
                        else
                        {
                            ngs.Add(this.ST5520Result);
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(this.H3153Result) && !this.H3153Result.Equals(EMPTY_VALUE) && !this.H3153Result.Equals("OK"))
                    {
                        if (short.TryParse(this.H3153Result, out shortValue))
                        {
                            ngs.Add(GetNGCodeDescByValue(shortValue));
                        }
                        else
                        {
                            ngs.Add(this.H3153Result);
                        }
                    }
                    ngs.RemoveAll(v => v.Equals(EMPTY_VALUE));
                    if (ngs.Contains("耐压NG") && ngs.Contains("耐压接触不良NG"))
                    {
                        ngs.RemoveAll(v => v.Equals("耐压NG"));
                    }
                    if (ngs.Contains("绝缘NG") && ngs.Contains("绝缘接触不良NG"))
                    {
                        ngs.RemoveAll(v => v.Equals("绝缘NG"));
                    }
                    try
                    {
                        ngs = ngs.Distinct().OrderBy(v => NGCODE_DESC_DIC[v]).ToList();
                        #region 把适合的扫码NG改成MES入站NG
                        //bool isMESInNG = !string.IsNullOrWhiteSpace(this.BarCode)
                        //    && !this.BarCode.Equals(MultiScanHelper.ERROR_BARCODE)
                        //    && ngs != null
                        //    && ngs.Contains("扫码NG");
                        //if (isMESInNG)
                        //{
                        //    var mesNGItemIndex = ngs.IndexOf("扫码NG");
                        //    if (mesNGItemIndex != -1)
                        //    {
                        //        ngs[mesNGItemIndex] = "MES入站NG";
                        //    }
                        //}
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        ngs = ngs.Distinct().ToList();
                    }
                    result = string.Join(",", ngs);
                }
                return result;
            }
        }

        /// <summary>
        /// 氦检结果
        /// </summary>
        [Description("氦检结果")]
        public string HeliumResult { get; set; } = "1";
        /// <summary>
        /// 漏率
        /// </summary>
        [Description("漏率(Pa·m³/s)")]
        public string LeakRate { get; set; } = "0.00E+00";
        /// <summary>
        /// 耐压判定结果
        /// </summary>
        [Description("耐压判定结果")]
        [GUIColumnWidth(Width = 200)]
        public string H3153Result { get; set; } = string.Empty;
        /// <summary>
        /// 耐压测试数据
        /// </summary>
        [Description("耐压测试数据(mA)")]
        public string Current { get; set; } = string.Empty;
        /// <summary>
        /// 绝缘判定结果
        /// </summary>
        [Description("绝缘判定结果")]
        [GUIColumnWidth(Width = 200)]
        public string ST5520Result { get; set; } = string.Empty;
        /// <summary>
        /// 绝缘测试数据
        /// </summary>
        [Description("绝缘测试数据(MΩ)")]
        public string Resistance { get; set; } = string.Empty;
        /// <summary>
        /// 腔体号
        /// </summary>
        [Description("腔体号")]
        public string HeCheckStation { get; set; } = "";
        /// <summary>
        /// 通道位置
        /// </summary>
        [Description("通道位置")]
        public string HeCheckStationNo { get; set; } = "";
        /// <summary>
        /// 复检腔体号
        /// </summary>
        [Description("复检腔体号")]
        public string ReCheckStation { get; set; } = "";
        /// <summary>
        /// 复检通道位置
        /// </summary>
        [Description("复检通道位置")]
        public string ReCheckStationNo { get; set; } = "";
        /// <summary>
        /// 氦检时间
        /// </summary>
        [Description("氦检时间")]
        public string HeliumTimes { get; set; } = "0";
        /// <summary>
        /// 检漏口压力
        /// </summary>
        [Description("检漏口压力(Pa)")]
        public string LeakPress { get; set; } = "0.00E+00";
        /// <summary>
        /// 腔体抽真空值
        /// </summary>
        [Description("腔体抽真空值")]
        public string RealValue { get; set; } = "0";
        /// <summary>
        /// 腔体抽真空时间
        /// </summary>
        [Description("腔体抽真空时间")]
        public string RealTimes { get; set; } = "0";
        /// <summary>
        /// 电池抽真空值
        /// </summary>
        [Description("电池抽真空值")]
        public string BatteryRealValue { get; set; } = "0";
        /// <summary>
        /// 电池抽真空时间
        /// </summary>
        [Description("电池抽真空时间")]
        public string BatteryRealTimes { get; set; } = "0";
        /// <summary>
        /// 注氦压力
        /// </summary>
        public string HeliumPress { get; set; } = "0";
        /// <summary>
        /// 注氦后真空值
        /// </summary>
        [Description("注氦后真空值")]
        public string BackHeliumPress { get; set; } = "0";
        /// <summary>
        /// 清氦真空值
        /// </summary>
        public string ClearHeliumPress { get; set; } = "0";
        /// <summary>
        /// 清氦次数
        /// </summary>
        public string HeliumClearTimes { get; set; } = "0";
        /// <summary>
        /// 质量
        /// </summary>
        public string Quality { get; set; } = string.Empty;
        /// <summary>
        /// 标记
        /// </summary>
        public string OutLabel { get; set; } = string.Empty;
        /// <summary>
        /// 复检标记
        /// </summary>
        public string ReCheckFlag { get; set; } = string.Empty;
        /// <summary>
        /// 是否OK出站 1-OK
        /// </summary>
        public string IsOKOut { get; set; } = "1";
        /// <summary>
        /// 是否离线数据
        /// </summary>
        [Description("MES状态")]
        public string IsMESOffline { get; set; } = "1";
        /// <summary>
        /// 是否已上传MES
        /// </summary>
        public string HasMESUpload { get; set; } = "0";
        /// <summary>
        /// 由字典数据生成产品对象
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static ProductInfo FromDic(Dictionary<string, object> dic)
        {
            ProductInfo info = new ProductInfo();
            if (dic != null)
            {
                PropertyInfo[] propertyInfos = info.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                foreach (PropertyInfo pi in propertyInfos)
                {
                    var name = pi.Name;
                    if (dic.ContainsKey(name))
                    {
                        var typeName = pi.PropertyType.Name.ToLower();
                        var objValue = dic[name];
                        if (objValue != null)
                        {
                            pi.SetValue(info, dic[name].ToString());
                        }
                    }
                }
            }
            return info;
        }
        private string GetNGCodeDescByValue(short shortValue)
        {
            string result = EMPTY_VALUE;
            var temp = NGCODE_DESC_DIC.Where(v => v.Value.Equals(shortValue)).ToList();
            if (temp != null && temp.Count() > 0)
            {
                result = temp[0].Key;
            }
            return result;
        }
        /// <summary>
        /// 格式化显示结果
        /// </summary>
        public void Format()
        {
            if (!string.IsNullOrWhiteSpace(this.BarCode))
            {
                int index = this.BarCode.IndexOf('\u0001');
                if (index != -1)
                {
                    this.BarCode = this.BarCode.Substring(0, index);
                }
            }

            this.Current = CommonHelper.JudgeFormatFloat(this.Current);
            this.Resistance = CommonHelper.JudgeFormatScientific(this.Resistance, 2, 2);
            this.LeakRate = CommonHelper.JudgeFormatScientific(this.LeakRate, 2, 2);
            this.LeakPress = CommonHelper.JudgeFormatScientific(this.LeakPress, 2, 2);
            this.HeliumTimes = CommonHelper.JudgeFormatFloat(this.HeliumTimes);
            this.BatteryRealTimes = CommonHelper.JudgeFormatFloat(this.BatteryRealTimes);
            this.RealTimes = CommonHelper.JudgeFormatFloat(this.RealTimes);
            this.HeliumPress = CommonHelper.JudgeFormatFloat(this.HeliumPress);
            this.BackHeliumPress = CommonHelper.JudgeFormatFloat(this.BackHeliumPress);
            this.BatteryRealValue = CommonHelper.JudgeFormatFloat(this.BatteryRealValue);

            if (string.IsNullOrWhiteSpace(this.ReCheckFlag) || !this.ReCheckFlag.Equals("1"))
            {
                this.ReCheckStation = EMPTY_VALUE;
                this.ReCheckStationNo = EMPTY_VALUE;
            }
            if (!string.IsNullOrWhiteSpace(this.IsMESOffline) && this.IsMESOffline.Equals("1"))
            {
                this.IsMESOffline = "离线";
            }
            else
            {
                this.IsMESOffline = "在线";
            }
            if (!string.IsNullOrWhiteSpace(this.HasMESUpload) && this.HasMESUpload.Equals("1"))
            {
                this.HasMESUpload = "已上传";
            }
            else
            {
                this.HasMESUpload = "未上传";
            }

            short shortValue = 0;
            if (!string.IsNullOrWhiteSpace(this.ST5520Result) && this.ST5520Result.Equals("100"))
            {
                this.ST5520Result = "OK";
            }
            else if (!string.IsNullOrWhiteSpace(this.ST5520Result) && short.TryParse(this.ST5520Result, out shortValue))
            {
                this.ST5520Result = GetNGCodeDescByValue(shortValue);
            }
            else
            {
                this.ST5520Result = EMPTY_VALUE;
            }

            if (!string.IsNullOrWhiteSpace(this.H3153Result) && this.H3153Result.Equals("100"))
            {
                this.H3153Result = "OK";
            }
            else if (!string.IsNullOrWhiteSpace(this.H3153Result) && short.TryParse(this.H3153Result, out shortValue))
            {
                this.H3153Result = GetNGCodeDescByValue(shortValue);
            }
            else
            {
                this.H3153Result = EMPTY_VALUE;
            }



            if (!string.IsNullOrWhiteSpace(this.HeliumResult) && this.HeliumResult.Equals("100"))
            {
                this.HeliumResult = "OK";
            }
            else if (!string.IsNullOrWhiteSpace(this.HeliumResult) && short.TryParse(this.HeliumResult, out shortValue))
            {
                this.HeliumResult = GetNGCodeDescByValue(shortValue);
            }
            else
            {
                this.HeliumResult = EMPTY_VALUE;
            }

            if (!string.IsNullOrWhiteSpace(this.HeliumResult) && this.HeliumResult.Equals(EMPTY_VALUE))
            {
                this.LeakRate = EMPTY_VALUE;
                this.LeakPress = EMPTY_VALUE;
            }
            else
            {
                this.LeakRate = CommonHelper.JudgeFormatScientific(this.LeakRate, 2, 2);
                this.LeakPress = CommonHelper.JudgeFormatScientific(this.LeakPress, 2, 2);
            }


            if (!string.IsNullOrWhiteSpace(this.H3153Result) && this.H3153Result.Equals(EMPTY_VALUE))
            {
                this.Current = EMPTY_VALUE;
            }
            if (!string.IsNullOrWhiteSpace(this.ST5520Result) && this.ST5520Result.Equals(EMPTY_VALUE))
            {
                this.Resistance = EMPTY_VALUE;
            }
        }
        /// <summary>
        /// 获取前景颜色
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public System.Drawing.Color GetForeColor(string name)
        {
            System.Drawing.Color result = System.Drawing.Color.White;
            if (!string.IsNullOrWhiteSpace(name))
            {
                if (((string.IsNullOrWhiteSpace(this.IsOKOut) || !this.IsOKOut.Equals("1")) && name.Equals("Id"))
                    || ((name.Equals("HeliumResult") || name.Equals("LeakRate") || name.Equals("LeakPress")) && !this.HeliumResult.Equals("OK") && !this.HeliumResult.Equals(EMPTY_VALUE))
                    || ((name.Equals("H3153Result") || name.Equals("Current")) && !this.H3153Result.Equals("OK") && !this.H3153Result.Equals(EMPTY_VALUE))
                    || ((name.Equals("ST5520Result") || name.Equals("Resistance")) && !this.ST5520Result.Equals("OK") && !this.ST5520Result.Equals(EMPTY_VALUE))
                    || (name.Equals("NGCode") && !this.NGCode.Equals(EMPTY_VALUE) && !this.NGCode.Equals("OK")))
                {
                    result = System.Drawing.Color.Red;
                }
            }
            return result;
        }
        /// <summary>
        /// 描述信息
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return CommonHelper.GetObjectString(this);
        }
    }
}

